package cz.fit.vutbr.pis2012.augeaswh.managers;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import cz.fit.vutbr.pis2012.augeaswh.persistence.Supplier;

@Stateless
public class SupplierManager {
	@PersistenceContext
	private EntityManager em;
	
	@SuppressWarnings("unchecked")
	public List<Supplier> getSuppliers() {
		return em.createQuery("SELECT s FROM Subject s Where s.type = :type")
				.setParameter("type", "SUPPLIER").getResultList();
	}
	
	public void save(Supplier supplier) {
		if(supplier.getBankAccount()!=null)
			em.merge(supplier.getBankAccount());
		em.merge(supplier);
	}
	
	public void delete(Supplier supplier) {
	    em.remove(em.merge(supplier));
	}
	
	public Supplier getSupplierByID(Long id) {
		@SuppressWarnings("unchecked")
		List<Supplier> list =  em.createQuery("SELECT s FROM Subject s Where s.type = :type AND s.id = :id")
									.setParameter("type", "SUPPLIER")
									.setParameter("id", id).getResultList();
		return (list.size() > 0) ? list.get(0) : null;
	}
	
}